package org.anupta.realm.server.realm.packet.account;

import org.anupta.realm.main.Main;
import org.anupta.realm.server.inter.client.InterClient;
import org.anupta.realm.server.inter.packet.account.AccountWaiting;
import org.anupta.realm.server.realm.client.RealmClient;
import org.anupta.realm.server.realm.packet.RealmPacket;

public class AccessServer extends RealmPacket {
	private int id = 0;

	public AccessServer(RealmClient client, String message) {
		super(client, message);
		unpack();
		pack();
		doPacket();
	}

	@Override
	public void unpack() {
		id = Integer.parseInt(packetIn.toString().substring(2));
	}

	@Override
	public void pack() {
		InterClient server = Main.inter.getClient(id);
		//TODO:UseIp (Crypt Ip + port)
		packetOut.append("AYK")
		.append(server.getIp())
		.append(":")
		.append(server.getPort())
		.append(";")
		.append(client.getAccount().getGuid());
	}

	@Override
	public void doPacket() {
		new AccountWaiting(Main.inter.getClient(id),client.getAccount());
		try {
			Thread.sleep(300);
			//We need to wait a little bit
			//before the game add the waiting account...
		} catch (InterruptedException e) {
			Main.err.write(e);
		}
		client.send(packetOut.toString());
		Main.out.println(new StringBuilder("AccessServer send to : ").append(client.getIp()).toString());
	}
}
